package com.ruoyi.web.controller.inv;

import com.ruoyi.buy.domain.entity.PO.InvOutboundBill;
import com.ruoyi.buy.domain.entity.PO.InvOutboundBillItem;
import com.ruoyi.buy.domain.entity.inv0005.Inv0005InvDataDto;
import com.ruoyi.buy.domain.entity.inv0005.Inv0005UpdateByIdVO;
import com.ruoyi.buy.service.IInvOutboundBillService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 出库单Controller
 * 
 * @author ruoyi
 * @date 2023-12-25
 */
@RestController
@RequestMapping("/inv/inv0005")
@Transactional
public class InvOutboundBillController extends BaseController
{
    @Autowired
    private IInvOutboundBillService invOutboundBillService;


    /**
     * 查询出库单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(InvOutboundBill invOutboundBill)
    {
        startPage();
        List<InvOutboundBill> list = invOutboundBillService.selectInvOutboundBillList(invOutboundBill);
        return getDataTable(list);
    }

    /**
     * 导出出库单列表
     */
    @Log(title = "出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvOutboundBill invOutboundBill)
    {
        List<InvOutboundBill> list = invOutboundBillService.selectInvOutboundBillList(invOutboundBill);
        ExcelUtil<InvOutboundBill> util = new ExcelUtil<InvOutboundBill>(InvOutboundBill.class);
        util.exportExcel(response, list, "出库单数据");
    }

    /**
     * 获取出库单详细信息
     */
    @PostMapping(value = "getInfo")
    public AjaxResult getInfo(@RequestBody InvOutboundBill para)
    {
        return success(invOutboundBillService.selectInvOutboundBillById(para.getId()));
    }

    /**
     * 新增出库单
     */
    @Log(title = "出库单", businessType = BusinessType.INSERT)
    @PostMapping(value = "addBill")
    public AjaxResult add(@RequestBody Inv0005UpdateByIdVO para)
    {
        return toAjax(invOutboundBillService.insertInvOutboundBill(para));
    }

    /**
     * 修改出库单
     */
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @PostMapping(value = "updateBill")
    public AjaxResult edit(@RequestBody Inv0005UpdateByIdVO para)
    {
        return toAjax(invOutboundBillService.updateInvOutboundBill(para));
    }

    /**
     * 修改出库单状态
     */
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @PostMapping(value = "updateBillStatus")
    public AjaxResult updateBillStatus(@RequestBody InvOutboundBill para)
    {
        return toAjax(invOutboundBillService.updateInvOutboundBillStatus(para));
    }

    /**
     * 删除出库单
     */
    @Log(title = "出库单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(invOutboundBillService.deleteInvOutboundBillByIds(ids));
    }

    /**
     * 查询出库单列表
     */
    @PostMapping("/getInvDataList")
    public TableDataInfo getInvDataList(@RequestBody Inv0005InvDataDto para)
    {
        startPage();
        List<Inv0005InvDataDto> list = invOutboundBillService.getInvDataList(para);
        return getDataTable(list);
    }

    /**
     * 删除出库单明细
     */
    @Log(title = "出库单明细", businessType = BusinessType.DELETE)
    @PostMapping("/delItem")
    public AjaxResult delItem(@RequestBody InvOutboundBillItem para)
    {
        return toAjax(invOutboundBillService.deleteInvOutboundBillItemById(para));
    }



    @PostMapping("/checkInvQty")
    public AjaxResult checkInvQty(@RequestBody InvOutboundBillItem para)
    {
        String message = invOutboundBillService.checkInvQty(para);
        if(("成功").equals(message)){
            return success("成功");
        }else{
            return error(message);
        }
    }

    @PostMapping("/executeOut")
    public AjaxResult executeOut(@RequestBody Inv0005UpdateByIdVO para) throws Exception {
        String message =invOutboundBillService.executeOut(para);
        if(("成功").equals(message)){
            return success("成功");
        }else{
            return error(message);
        }
    }

    @PostMapping("/viewCKBillById")
    public AjaxResult printOut(@RequestBody InvOutboundBill para) throws Exception {
        return success(invOutboundBillService.printOut(para));
    }

    @PostMapping("/getApplys")
    public AjaxResult getApplys(@RequestBody Map<String,String> para) throws Exception {
        List<Map> list = this.invOutboundBillService.getApplys(para.get("value"));
        return success(list);
    }
}
